#!/bin/bash
#第一个参数192.168.1.166为虚拟的vip ,第二个为api-server的地址
#脚本运行命令: ./keepalived.sh 192.168.1.166 192.168.1.208
VIP_ADDRESS=${1:-"192.168.1.166"}
MASTER_ADDRESS=${2:-"192.168.1.208"}
yum install -y haproxy keepalived
> /etc/keepalived/keepalived.conf
cat <<EOF >/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email {
        test@sina.com
    }
    notification_email_from admin@test.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30 
    router_id LVS_MASTER
}

vrrp_script check_haproxy {
    script "/etc/keepalived/check_haproxy.sh"
    interval 3
}

vrrp_instance VI_1 {
    state MASTER  # 如果配置主从，从服务器改为BACKUP即可
    interface ens33
    virtual_router_id 60
    priority 100  # 从服务器设置小于100的数即可
    advert_int 1
    authentication { 
        auth_type PASS 
        auth_pass 1111
    }
    virtual_ipaddress {
        ${VIP_ADDRESS}/24
    }

    track_script {
        check_haproxy
    }
}
EOF
> /lib/systemd/system/keepalived.service
cat <<'EOF' >/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target haproxy.service
Require=kube-apiserver.service

[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
 
[Install]
WantedBy=multi-user.target
EOF

cat <<'EOF' >/etc/keepalived/check_haproxy.sh
#!/bin/bash
flag=$(systemctl status haproxy &> /dev/null;echo $?)
if [[ $flag != 0 ]];then
    echo "haproxy is down,close the keepalived"
    systemctl stop keepalived
fi
EOF
> /etc/haproxy/haproxy.cfg
cat <<EOF >/etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats

defaults
    mode                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option                  redispatch
    retries                 3
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout check           10s
    maxconn                 3000

listen stats
    mode   http
    bind :10086
    stats   enable
    stats   uri     /admin?stats
    stats   auth    admin:admin
    stats   admin   if TRUE

frontend  k8s_https *:8443
    mode      tcp
    maxconn      2000
    default_backend     https_sri

backend https_sri
    balance      roundrobin
    server s1 ${MASTER_ADDRESS}:6443  check inter 10000 fall 2 rise 2 weight 1
EOF
chmod +x /etc/keepalived/check_haproxy.sh
systemctl daemon-reload
systemctl enable haproxy
systemctl enable keepalived
systemctl start haproxy
systemctl start keepalived
